//
//  BaseUIVC.m
//  MyFamework
//
//  Created by carlos on 13-4-17.
//  Copyright (c) 2013年 carlos. All rights reserved.
//

#import "BaseUIVC.h"
#import "MBProgressHUD.h"


@implementation BaseUIVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        [self initCONavWithTitle:@"aaaaa"];
    }
    return self;
}

- (void)pushToNextVC:(UIViewController *)nextVC{
    [self.navigationController pushViewController:nextVC animated:YES];
    return;

}
//返回上一个界面
- (void)popVC{
    [self popVC:1];
}
//返回前面几个界面
- (void)popVC:(int)vcNum{
    NSInteger viewControllerCount = [self.navigationController.viewControllers count];
    
    if(viewControllerCount >= vcNum+1){
        BaseAtomVC *previousVC = [self.navigationController.viewControllers objectAtIndex:viewControllerCount-vcNum-1];
        
        [self.navigationController popToViewController:previousVC animated:YES];
        return;
//        [self.view.superview addSubview:previousVC.view];
//        [self.view.superview sendSubviewToBack:previousVC.view];
//        previousVC.view.hidden = YES;
        previousVC.view.transform = KVCTransform;
        [UIView animateWithDuration:KVCAnimDuration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             previousVC.view.transform = CGAffineTransformIdentity;
                             self.view.frame = kFrameWithX(kFrameAllNoStatus,kFBaseWidth);

                         }
                         completion:^(BOOL finish){
                             
                             //                             [previousVC.view removeFromSuperview];
                             //                             previousVC.view = nil;
//                             self.view.hidden = YES;
//                             [self.view removeFromSuperview];
                             for (UIView *eachView in self.view.subviews) {
                                 eachView.hidden = YES;
//                                 [eachView removeFromSuperview];
                             }
                             CLog(@"%@",self);
                             self.view.hidden = YES;
                             self.view = nil;
                             [self viewDidPop];
                             
//                             for (int i = 0; i < vcNum; i++) {
//                                 [self.navigationController popViewControllerAnimated:NO];
//                             }

                             [self.navigationController popToViewController:previousVC animated:NO];
                             [self removeFromParentViewController];


                         }];
    }

}
-(IBAction)onClickWithXib:(UIView *)sender{
    
    if (![self onClick:sender]) {
        // 如果为NO，则自行处理

    }
}
//当界面退出的时候要做的事情
-(void)viewDidPop{
    
}
//点击了某个按钮
-(BOOL)onClick:(UIView *)sender{
    return NO;
}

//即将显示
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}


-(BOOL)isTopVC{
    NSInteger viewControllerCount = [self.navigationController.viewControllers count];
    
    if(viewControllerCount >= 2){
        BaseAtomVC *previousVC = [self.navigationController.viewControllers objectAtIndex:0];
        return  self == previousVC;
    }else{
        return YES;
    }

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IOS7_OR_LATER) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    
}

#pragma mark 风火轮
//显示风火轮
- (void)showProgressView
{
    MBProgressHUD *progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    [progress hide:NO afterDelay:5];
}

//停止风火轮
- (void)hideProgressView
{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

#pragma mark 弹出框
//显示提示信息
- (void)alertMsg:(NSString *)msg{
    [self alertMsg:msg withEvent:^(){
        CLog(@"点击了确定按钮");
    }];
}
//显示提示信息
- (void)alertMsg:(NSString *)msg withEvent:(BaseOnClickOne )onClickConfrim
{

    [ViewUtils showAlertWithMsg:msg withEvent:onClickConfrim];
}
//取消和确定对话框显示提示信息
- (void)alert2BtnMsg:(NSString *)msg withEvent:(BaseOnClickOne )onClickConfrim{
    NSArray *btns = @[kWCancel,kWConfrim];
    [ViewUtils showAlertWithMsg:msg withBtns:btns withEvent:^(int index){
        if (index == 1) {
            onClickConfrim();
        }
    }];
}
@end
